package com.museum.controller;

import com.museum.common.Result;
import com.museum.entity.SystemSettings;
import com.museum.entity.Exhibition;
import com.museum.service.ExhibitionService;
import com.museum.service.ReservationService;
import com.museum.service.SystemSettingsService;
import com.museum.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 统计数据控制器
 */
@RestController
@RequestMapping("/statistics")
public class StatisticsController {

    @Autowired
    private UserService userService;

    @Autowired
    private ReservationService reservationService;
    
    @Autowired
    private ExhibitionService exhibitionService;
    
    @Autowired
    private SystemSettingsService systemSettingsService;
    
    /**
     * 获取系统设置信息
     * @return 返回博物馆基本信息
     */
    @GetMapping("/museum-info")
    public Result<SystemSettings> getMuseumInfo() {
        SystemSettings settings = systemSettingsService.getSettings();
        if (settings == null) {
            return Result.error("未找到博物馆信息");
        }
        return Result.success(settings);
    }
    
    /**
     * 获取概览数据
     */
    @GetMapping("/overview")
    public Result<Map<String, Object>> getOverview() {
        // 统计总数
        long userCount = userService.count();
        long exhibitionCount = exhibitionService.count();
        long reservationCount = reservationService.count();
        
        Map<String, Object> data = new HashMap<>();
        data.put("userCount", userCount);
        data.put("exhibitionCount", exhibitionCount);
        data.put("reservationCount", reservationCount);
        
        return Result.success(data);
    }
    
    /**
     * 获取每日预约人数统计
     */
    @GetMapping("/daily-reservations")
    public Result<Map<String, Object>> getDailyReservations(@RequestParam(value = "days", defaultValue = "7") Integer days) {
        List<String> dateList = new ArrayList<>();
        List<Integer> countList = new ArrayList<>();
        
        // 获取最近n天的日期
        LocalDate endDate = LocalDate.now();
        LocalDate startDate = endDate.minusDays(days - 1);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        
        for (LocalDate date = startDate; !date.isAfter(endDate); date = date.plusDays(1)) {
            String dateStr = date.format(formatter);
            dateList.add(dateStr);
            
            // 统计每天的预约数量
            int count = reservationService.countByDate(dateStr);
            countList.add(count);
        }
        
        Map<String, Object> data = new HashMap<>();
        data.put("dates", dateList);
        data.put("counts", countList);
        
        return Result.success(data);
    }
    
    /**
     * 获取热门参观日期分析
     */
    @GetMapping("/popular-dates")
    public Result<List<Map<String, Object>>> getPopularDates() {
        // 获取热门日期数据
        List<Map<String, Object>> popularDatesList = reservationService.getPopularDates();
        return Result.success(popularDatesList);
    }
    
    /**
     * 获取用户增长趋势
     */
    @GetMapping("/user-growth")
    public Result<Map<String, Object>> getUserGrowth(@RequestParam(value = "months", defaultValue = "6") Integer months) {
        List<String> monthList = new ArrayList<>();
        List<Integer> countList = new ArrayList<>();
        
        // 获取最近n个月的数据
        LocalDate now = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
        
        for (int i = months - 1; i >= 0; i--) {
            LocalDate date = now.minusMonths(i);
            String monthStr = date.format(formatter);
            monthList.add(monthStr);
            
            // 统计每月新增用户数
            int count = userService.countByMonth(monthStr);
            countList.add(count);
        }
        
        Map<String, Object> data = new HashMap<>();
        data.put("months", monthList);
        data.put("counts", countList);
        
        return Result.success(data);
    }

    /**
     * 获取展览热度分析
     */
    @GetMapping("/exhibition-popularity")
    public Result<List<Map<String, Object>>> getExhibitionPopularity() {
        // 模拟展览热度数据，实际项目中可以从数据库中查询
        List<Map<String, Object>> data = new ArrayList<>();
        
        // 获取所有展览
        List<Exhibition> exhibitions = exhibitionService.list();
        
        // 为每个展览创建数据
        for (Exhibition exhibition : exhibitions) {
            if (exhibition.getStatus() == 1) { // 只统计上架的展览
                Map<String, Object> item = new HashMap<>();
                item.put("name", exhibition.getTitle());
                
                // 在实际项目中，这里可以查询实际的预约数量
                // 这里使用模拟数据
                Long reservationCount = Math.round(Math.random() * 100);
                
                item.put("value", reservationCount);
                data.add(item);
            }
        }
        
        return Result.success(data);
    }
} 